<?php

if (! defined('BASEPATH'))
    exit('No direct script access allowed');

class MY_Controller extends CI_Controller
{

    /**
     * 用户数据
     * 
     * @var Array
     */
    protected $p = array();

    /**
     * 表名
     * 
     * @var Object
     */
    protected $tbl;
    
    function __construct ()
    {
        parent::__construct();

        // 加载smarty
        $this->load->library('smarty', array(
            'template_dir'=>APPDIR.'views/',
            'compile_dir'=>APPDIR.'cache/smarty/views_c/',
            'config_dir'=>APPDIR.'config/',
            'cache_dir'=>APPDIR.'cache/smarty/'
        ));
        
        // 取得所有表
        $this->tbl = (object) config_item('table');
        
        // 通用用户数据处理
        $uservd = array();
        foreach ($_REQUEST as $k => $v) {
            if (is_string($v)) {
                $v = trim($v);
            }
            $uservd[$k] = $v;
        }
        $this->p = $uservd;
        
        $this->smarty->assign('base_url',base_url());
        $this->smarty->assign('statics_url',base_url() . 'statics/');
        
        if(!$this->is_ajax_request()){
            $this->prss_menu();
            $this->smarty->display('inc/header');        
        }
        $this->load->model('common_model','cm');

    }
    
    private function prss_menu(){
        $menuconfig = simplexml_load_string(file_get_contents(CFDIR.'menu.xml'));
        
        $top_menu = array();
        
        foreach ($menuconfig->model as $m){
            $model_item = array();    
            $group = array();
            foreach ($m->group as $g){
                $attrs = $g->attributes();
                $group_item = array();
                $group_item['id'] = (string)$attrs->id;
                $group_item['title'] = (string)$attrs->name;
                $group_item['menu'] = $this->get_menu_by_group($g);
                $group[] = $group_item;
            }
            
            $model_item['id'] = (string)$m->attributes()->id;
            $model_item['title'] = (string)$m->attributes()->name;
            $model_item['group'] = $group;
            $model_item['ct'] = ($this->getParam('mdl') && $this->getParam('mdl') == $model_item['id']) ? 'c' : '';
            
            $top_menu[$model_item['id']] = $model_item;
        }
        
        if($this->getParam('mdl')){
            $this->smarty->assign('cmodel',$top_menu[$this->getParam('mdl')]);
        } 
        $this->smarty->assign('top_menu',$top_menu);
        
    }
    
    private function get_menu_by_group($g){
        $menu = array();
        foreach ($g->menu as $m){
            $item = array();
            $attrs = $m->attributes();
            $item['id'] = (string)$attrs->id;
            $item['ctl'] = (string)$attrs->ctl;
            $item['act'] = (string)$attrs->act;
            $item['display'] = (string)$attrs->display;
            $item['title'] = (string)$m;
            $item['ct'] = ($this->getParam('menu') && $this->getParam('menu') == $item['id']) ? 'c' : '';
            
            $menu[] = $item;
        }
        return $menu;        
    }    

    private function get_sub_ctl($fctl,$menu){
        
        $arr_sub_ctl = array();
        $arr_sub_ctl[] = $fctl;
        
        if(!empty($menu['sub'])){
            foreach ($menu['sub'] as $k=>$v){
                $arr_sub_ctl[] = $v['ctl'];
                
                if(!empty($v['sub'])){
                    foreach ($v['sub'] as $kk=>$vv){
                        $arr_sub_ctl[] = $vv['ctl'];
                    }
                }
            }
        }
        
        return $arr_sub_ctl;
    }
    
    function is_ajax_request(){
        return !empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest';
    }
    
    public function getParam($name,$default=''){
        if(!empty($_REQUEST[$name])){
            if(!is_array($_REQUEST[$name])) return trim($_REQUEST[$name]); 
        }
        
        if(!empty($default)) return $default;
        
        return '';
    }
    
    function upload ()
    {
        require_once LIB_PATH . 'thumb/ThumbLib.inc.php';
        
        define('PUBLIC_DIR', dirname(__FILE__) . '/public');
        define('IMG_DIR', PUBLIC_DIR . '/images');
        define('IMG_PATH', 'public/images');
        
        if (empty($_FILES['Filedata']['name']))
        
        $arr_filename = explode('.', $_FILES['Filedata']['name']);
        $extname = $arr_filename[count($arr_filename) - 1];
        $extname = strtoupper($extname);
        
        if (! in_array($extname, array(
                'JPG',
                'GIF',
                'PNG'
        )))
        
        $rname = rand_filename();
        $ofname = 'O_' . $rname . '.' . $extname;
        $tfname = 'B_' . $rname . '.' . $extname;
        $lfname = 'L_' . $rname . '.' . $extname;
        
        $opath = IMG_PATH . '/' . $ofname;
        $tpath = IMG_PATH . '/' . $tfname;
        $lpath = IMG_PATH . '/' . $lfname;
        
        $tempFile = $_FILES['Filedata']['tmp_name'];
        
        $conf_thumb = config_item('thumb');
        
        $thumb = PhpThumbFactory::create($tempFile);
        $thumb->resize($conf_thumb['w']);
        $thumb->save($tpath);
        
        $thumb = PhpThumbFactory::create($tempFile);
        $thumb->adaptiveResize($conf_thumb['lw'], $conf_thumb['lh']);
        $thumb->save($lpath);
        
        @move_uploaded_file($tempFile, $opath);
        
        $id = $this->cm->save('images', 
                array(
                        'o' => $opath,
                        't' => $tpath,
                        'l' => $lpath
                ));
        
        $base_url = config_item('base_url');
        echo json_encode(
                array(
                        'spath' => $base_url . $lpath,
                        'id' => $id
                ));
        
        exit();
    }

    function avatar ()
    {
        require_once LIB_PATH . 'thumb/ThumbLib.inc.php';
        $c = $this->config->item('avatar');
        
        $arr_filename = explode('.', $_FILES['Filedata']['name']);
        $extname = $arr_filename[count($arr_filename) - 1];
        $new_filename = rand_filename() . '.' . $extname;
        
        $small_file_path = SAVATAR_DIR . $new_filename;
        $mdl_file_path = MAVATAR_DIR . $new_filename;
        $file_path = AVATAR_DIR . $new_filename;
        
        $upload_small_file_path = 'upload/avatar/s/' . $new_filename;
        $upload_mdl_file_path = 'upload/avatar/m/' . $new_filename;
        $upload_file_path = 'upload/avatar/' . $new_filename;
        
        $data = array();
        $tempFile = $_FILES['Filedata']['tmp_name'];
        
        $thumb = PhpThumbFactory::create($tempFile);
        $thumb->adaptiveResize($c['sw'], $c['sh']);
        $thumb->save($small_file_path);
        
        $thumb = PhpThumbFactory::create($tempFile);
        $thumb->adaptiveResize($c['mw'], $c['mh']);
        $thumb->save($mdl_file_path);
        
        $thumb = PhpThumbFactory::create($tempFile);
        $thumb->resize($c['bw']);
        $thumb->save($file_path);
        
        $data['small_path'] = $upload_small_file_path;
        $data['mdl_path'] = $upload_mdl_file_path;
        $data['path'] = $upload_file_path;
        
        $id = $this->cm->save($this->tbl->avatar, $data);
        
        echo json_encode(
                array(
                        'id' => $id,
                        'spath' => config_item('base_url') .
                                 $upload_small_file_path
                ));
    }
    
    public function __destruct(){
        if(!$this->is_ajax_request()){
            $this->smarty->display('inc/footer');
        }
    }
    
}
